<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------
use think\cache\driver\Redis;
use think\Session;
use think\Db;
use app\index\model\vo_video_stats;

// 应用公共文件
define('IS_GET', $_SERVER['REQUEST_METHOD'] == 'GET' ? true : false);
define('IS_POST', $_SERVER['REQUEST_METHOD'] == 'POST' ? true : false);


function get_vod_config()
{
    $debug = false;
    if ($debug) {
        return config('tencent.my');
    } else {
        return config('tencent.sw');
    }
}

function url_key($fileUrl, $exp = null)
{
    $config = get_vod_config();
    $video_key = $config['video_key'];
    $video_domain = $config['video_domain'];
    if (!$video_key) {
        return $video_domain . $fileUrl;
    }

    $arr = explode('/', $fileUrl);
    $Dir = '/' . $arr[0] . '/' . $arr[1] . '/';
    $t = dechex(time() + 60 * 60 * 1); //一个小时后播放地址失效
    $us = "72d4cd1101";

    if ($exp) {//允许播放时长
        $sign = md5($video_key . $Dir . $t . $exp . $us);
        $url = $video_domain . $fileUrl . "?t=$t&exper=$exp&us=$us&sign=$sign";
    } else {
        $sign = md5($video_key . $Dir . $t . $us);
        $url = $video_domain . $fileUrl . "?t=$t&us=$us&sign=$sign";
    }

    return $url;
}

function is_login($channel = 0)
{
    //根据渠道统计用户数(h5、web、网维或者桌面秀-在线)
    if (!session('isLogin')) {
        session('isLogin', true);

        $vo_video_stats = new vo_video_stats();
        $vo_video_stats->where(['time' => date("Y-m-d"), 'channel' => $channel])->setInc('user');
    }
}

function is_login2($channel = 0)
{
    //根据渠道统计用户数(网维或者桌面秀-本地)
    if (!session('isLogin2')) {
        session('isLogin2', true);

        $vo_video_stats = new vo_video_stats();
        $vo_video_stats->where(['time' => date("Y-m-d"), 'channel' => $channel])->setInc('user');
    }
}

function s_to_hs($s = 0)
{
    //计算分钟
    //算法：将秒数除以60，然后下舍入，既得到分钟数
    $h = floor($s / 60);
    //计算秒
    //算法：取得秒%60的余数，既得到秒数
    $s = $s % 60;
    //如果只有一位数，前面增加一个0
    $h = (strlen($h) == 1) ? '0' . $h : $h;
    $s = (strlen($s) == 1) ? '0' . $s : $s;
    return $h . ':' . $s;
}

function msubstr($str, $start = 0, $length, $charset = "utf-8", $suffix = false)
{
    //中文字符串截取
    if (function_exists("mb_substr")) {
        if ($suffix)
            return mb_substr($str, $start, $length, $charset) . "...";
        else
            return mb_substr($str, $start, $length, $charset);
    } elseif (function_exists('iconv_substr')) {
        if ($suffix)
            return iconv_substr($str, $start, $length, $charset) . "...";
        else
            return iconv_substr($str, $start, $length, $charset);
    }
    $re['utf-8'] = "/[x01-x7f]|[xc2-xdf][x80-xbf]|[xe0-xef][x80-xbf]{2}|[xf0-xff][x80-xbf]{3}/";
    $re['gb2312'] = "/[x01-x7f]|[xb0-xf7][xa0-xfe]/";
    $re['gbk'] = "/[x01-x7f]|[x81-xfe][x40-xfe]/";
    $re['big5'] = "/[x01-x7f]|[x81-xfe]([x40-x7e]|xa1-xfe])/";
    preg_match_all($re[$charset], $str, $match);
    $slice = join("", array_slice($match[0], $start, $length));
    if ($suffix) return $slice . "…";
    return $slice;
}

function filter_Emoji($str)
{
    $str = preg_replace_callback(    //执行一个正则表达式搜索并且使用一个回调进行替换
        '/./u',
        function (array $match) {
            return strlen($match[0]) >= 4 ? '' : $match[0];
        },
        $str);

    return $str;
}

function redis_init($select = 3)
{
    $_redis = new Redis(['select' => $select]);
    return $_redis->handler();
}

function session_init($select = 1)
{
    $config = config('session');
    $config['select'] = $select;
    Session::init($config);
}

function login_check($is_post = false)
{
    //登录校验
    $user_token = cookie('user_token');
    $user = Db::table('vo_app_user')->where(['token' => $user_token])->find();
    if (!$user) {
        cookie('user_token', null);
        if ($is_post) {
            header('Content-Type:application/json; charset=utf-8');
            exit(json_encode(['code' => 500, 'msg' => '该用户不存在，请登录!']));
        } else {
            $url = config('_download.url');
            echo "该用户不存在，请登录! <br><a href='{$url}'>如果疑问，请及时下载最新版本体验</a>";
            exit();
        }
    }

    return $user;
}